package com.aba.im.pojo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserAuthentication {
    private JFrame registerFrame;
    private JFrame loginFrame;

    public UserAuthentication() {
        initRegisterFrame();
        initLoginFrame();
    }

    private void initRegisterFrame() {
        registerFrame = new JFrame("User Registration");
        registerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        registerFrame.setSize(300, 200);
        registerFrame.setLayout(new GridLayout(3, 2));

        JLabel usernameLabel = new JLabel("Username:");
        JTextField usernameField = new JTextField();
        JLabel passwordLabel = new JLabel("Password:");
        JPasswordField passwordField = new JPasswordField();
        JButton registerButton = new JButton("Register");

        registerFrame.add(usernameLabel);
        registerFrame.add(usernameField);
        registerFrame.add(passwordLabel);
        registerFrame.add(passwordField);
        registerFrame.add(new JLabel());
        registerFrame.add(registerButton);

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                // 这里简单打印注册信息，实际可连接数据库等操作
                System.out.println("Registered: Username - " + username + ", Password - " + password);
                registerFrame.setVisible(false);
                loginFrame.setVisible(true);
            }
        });

        registerFrame.setVisible(true);
    }

    private void initLoginFrame() {
        loginFrame = new JFrame("User Login");
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.setSize(300, 200);
        loginFrame.setLayout(new GridLayout(3, 2));

        JLabel usernameLabel = new JLabel("Username:");
        JTextField usernameField = new JTextField();
        JLabel passwordLabel = new JLabel("Password:");
        JPasswordField passwordField = new JPasswordField();
        JButton loginButton = new JButton("Login");

        loginFrame.add(usernameLabel);
        loginFrame.add(usernameField);
        loginFrame.add(passwordLabel);
        loginFrame.add(passwordField);
        loginFrame.add(new JLabel());
        loginFrame.add(loginButton);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                // 这里简单打印登录信息，实际可进行数据库验证等操作
                System.out.println("Login attempt: Username - " + username + ", Password - " + password);
                loginFrame.setVisible(false);
                new ClientMain();
            }
        });

        loginFrame.setVisible(false);
    }

    public static void main(String[] args) {
        new UserAuthentication();
    }
}
